package com.future.mayi.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.ActivitiesActivity;
import com.future.mayi.model.IndexActivityModel;
import com.future.mayi.utils.HCUtils;

import java.util.List;

public class AllActivityView extends LinearLayout {
    private List<IndexActivityModel> data;
    private LayoutInflater mInflater;
    private Context mContext;

    public AllActivityView(Context context) {
        this(context, null);
    }

    public AllActivityView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AllActivityView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        setOrientation(VERTICAL);
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<IndexActivityModel> data) {
        this.data = data;
        removeAllViews();
        if (data == null || data.size() == 0) return;
        for (IndexActivityModel model : data) {
            View view = mInflater.inflate(R.layout.layout_index_item, this, false);
            addView(view);

            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.width = CommonUtils.getScreenSize(mContext)[0];
                layoutParams.height = (int) ((float) layoutParams.width * 0.53f);
            } else {
                view.setLayoutParams(new ViewGroup.LayoutParams(CommonUtils.getScreenSize(mContext)[0], (int) ((float) CommonUtils.getScreenSize(mContext)[0] * 0.53f)));
            }


            TextView tvTitle = view.findViewById(R.id.tv_title);
            ImageView ivImg = view.findViewById(R.id.iv_img);
            setText(tvTitle, model.getActivity_name());
            HCUtils.loadWebImg(mContext, ivImg, model.getActivity_img());
            view.setTag(model);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getTag() == null) return;
                    if (!(v.getTag() instanceof IndexActivityModel)) return;
                    ActivitiesActivity.startActivity(mContext, ((IndexActivityModel) v.getTag()).getActivity_id());
                }
            });
        }
    }

    protected void setText(TextView textView, CharSequence content) {
        if (textView == null)
            return;
        textView.setText(TextUtils.isEmpty(content) ? "" : content);
    }

}
